// pages/teacher/kechenbiao/detail.js
Page({
  data: {
    courseId: null,
    courseInfo: null,
    studentList: [],
    attendanceStats: {
      total: 0,
      present: 0,
      absent: 0,
      leave: 0,
      rate: '0%'
    },
    isLoading: true,
    activeTab: 0, // 0: 课程信息, 1: 学生名单, 2: 考勤记录
    attendanceList: []
  },

  onLoad: function(options) {
    if (options.id) {
      this.setData({
        courseId: options.id
      });
      this.fetchCourseDetail();
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },
  
  // 获取课程详情
  fetchCourseDetail: function() {
    this.setData({ isLoading: true });
    
    // 模拟数据
    setTimeout(() => {
      const mockCourseInfo = {
        id: this.data.courseId,
        courseName: '高等数学',
        courseCode: 'MATH101',
        credit: 4,
        className: '计算机科学1班',
        semester: '2023-2024学年第一学期',
        schedule: [
          { day: '周一', time: '08:00-09:40', location: '教学楼A-301' },
          { day: '周三', time: '10:00-11:40', location: '教学楼A-301' }
        ],
        studentCount: 45,
        startDate: '2023-09-01',
        endDate: '2024-01-15',
        description: '本课程主要讲授微积分、线性代数等高等数学基础知识，培养学生的数学思维和解决问题的能力。'
      };
      
      const mockStudentList = [];
      for (let i = 1; i <= 45; i++) {
        mockStudentList.push({
          id: i,
          studentId: `2023${i.toString().padStart(3, '0')}`,
          name: `学生${i}`,
          gender: i % 2 === 0 ? '女' : '男',
          attendanceRate: Math.floor(85 + Math.random() * 15) + '%'
        });
      }
      
      const mockAttendanceList = [
        {
          id: 1,
          date: '2023-12-10',
          time: '08:00-09:40',
          type: '定位签到',
          totalCount: 45,
          presentCount: 42,
          absentCount: 2,
          leaveCount: 1,
          status: '已结束'
        },
        {
          id: 2,
          date: '2023-12-07',
          time: '10:00-11:40',
          type: '二维码签到',
          totalCount: 45,
          presentCount: 43,
          absentCount: 1,
          leaveCount: 1,
          status: '已结束'
        },
        {
          id: 3,
          date: '2023-12-05',
          time: '08:00-09:40',
          type: '手势签到',
          totalCount: 45,
          presentCount: 40,
          absentCount: 3,
          leaveCount: 2,
          status: '已结束'
        },
        {
          id: 4,
          date: '2023-12-03',
          time: '10:00-11:40',
          type: '定位签到',
          totalCount: 45,
          presentCount: 44,
          absentCount: 0,
          leaveCount: 1,
          status: '已结束'
        },
        {
          id: 5,
          date: '2023-11-29',
          time: '08:00-09:40',
          type: '二维码签到',
          totalCount: 45,
          presentCount: 41,
          absentCount: 2,
          leaveCount: 2,
          status: '已结束'
        }
      ];
      
      // 计算考勤统计
      const totalAttendance = mockAttendanceList.length * 45;
      const presentAttendance = mockAttendanceList.reduce((sum, item) => sum + item.presentCount, 0);
      const absentAttendance = mockAttendanceList.reduce((sum, item) => sum + item.absentCount, 0);
      const leaveAttendance = mockAttendanceList.reduce((sum, item) => sum + item.leaveCount, 0);
      const attendanceRate = (presentAttendance / totalAttendance * 100).toFixed(1) + '%';
      
      this.setData({
        courseInfo: mockCourseInfo,
        studentList: mockStudentList,
        attendanceList: mockAttendanceList,
        attendanceStats: {
          total: totalAttendance,
          present: presentAttendance,
          absent: absentAttendance,
          leave: leaveAttendance,
          rate: attendanceRate
        },
        isLoading: false
      });
    }, 500);
  },
  
  // 切换标签页
  switchTab: function(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    
    // 防止重复点击当前标签
    if (this.data.activeTab === index) {
      return;
    }
    
    // 先设置数据，然后使用nextTick确保DOM更新完成
    this.setData({
      activeTab: index
    }, () => {
      // 如果是学生名单标签且还没有加载过数据
      if (index === 1 && this.data.studentList.length === 0) {
        this.fetchStudentList();
      }
      
      // 如果是考勤记录标签且还没有加载过数据
      if (index === 2 && this.data.attendanceList.length === 0) {
        this.fetchAttendanceList();
      }
    });
  }, 
  
  // 查看考勤详情
  viewAttendanceDetail: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/teacher/kaoqin/detail?id=${id}&courseId=${this.data.courseId}`
    });
  },
  
  // 发起考勤
  startAttendance: function() {
    wx.navigateTo({
      url: `/pages/teacher/kaoqin/faqi/faqi?courseId=${this.data.courseId}`
    });
  },
  
  // 导出学生名单
  exportStudentList: function() {
    wx.showLoading({
      title: '导出中...',
    });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showModal({
        title: '导出成功',
        content: '学生名单已导出，请到系统后台下载',
        showCancel: false
      });
    }, 1500);
  },
  
  // 导出考勤记录
  exportAttendanceList: function() {
    wx.showLoading({
      title: '导出中...',
    });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showModal({
        title: '导出成功',
        content: '考勤记录已导出，请到系统后台下载',
        showCancel: false
      });
    }, 1500);
  }
});